import {MessageBox, Message} from 'element-ui';

export function handleRequest({method, data, operateType, warnAlertShow, successAlertShow, failAlertShow, warnAlertCancelCall, successCall, failCall}) {
  if (!operateType) {
    operateType = '操作'
  }
  let param = {
    method,
    data,
    successAlertShow,
    successAlertTitle: operateType + '成功！',
    successCall,
    failAlertShow,
    failAlertTitle: operateType + '失败！',
    failCall
  };
  if (warnAlertShow) {
    MessageBox.confirm('确定' + operateType + '？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
      customClass: 'massageBoxTop20',
      confirmButtonLoading: false,
      showClose: false,
      closeOnClickModal: false,
      beforeClose: (action, instance, done) => {
        if (action === 'confirm') {
          instance.confirmButtonLoading = true;
          instance.confirmButtonText = '执行中...';
          param.done = done;
          handle(param);
        } else {
          done();
        }
      }
    }).then(() => {
    }).catch(() => {
      alert("取消")
      if (warnAlertCancelCall) {
        warnAlertCancelCall();
      }
    });
  } else {
    handle(param);
  }
}

function handle({method, data, successAlertShow, successAlertTitle, successCall, failAlertShow, failAlertTitle, failCall, done}) {
  method(data).then(result => {
    done && done();
    let {code, msg, data} = result;
    if (code == 10000) {
      if (successAlertShow) {
        Message.success(successAlertTitle);
      }
      if (successCall) {
        successCall(data);
      }
    } else {
      if (failAlertShow) {
        Message.error(failAlertTitle + ":" + msg);
      }
      if (failCall) {
        failCall(data);
      }
    }
  }).catch((result) => {
    done && done();
    Message.error("系统异常")
    console.log("系统异常", result)
  })
}


//====================================================================================

export function toTree(data) {
  console.log(data);
  return doToTree(null, data);
}

function doToTree(pid, rows) {
  let children = rows.filter(row => row.pid == pid)
  children.forEach(row => {
    row.children = doToTree(row.id, rows);
  })
  return children;
}

//====================================================================================


export function enumData() {
  return {
    elevatorType: [
      {key: 1, value: "自动人行道"},
      {key: 2, value: "杂物电梯"},
      {key: 3, value: "曳引与强制驱动电梯"},
      {key: 4, value: "自动扶梯"},
      {key: 5, value: "液压驱动电梯"}
    ],
    gender: [
      {key: 0, value: "男"},
      {key: 1, value: "女"}
    ],
    active: [
      {key: 0, value: "停用"},
      {key: 1, value: "启用"}
    ],
    exits: [
      {key: 0, value: "无"},
      {key: 1, value: "有"}
    ],
    education: [
      {key: 1, value: "博士"},
      {key: 2, value: "硕士"},
      {key: 3, value: "本科"},
      {key: 4, value: "大专"},
      {key: 5, value: "中专"},
      {key: 6, value: "高中"},
      {key: 7, value: "初中"},
      {key: 8, value: "小学"},
      {key: 9, value: "其它"},
    ]

  }
};
